﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.TreatmentHistory
{
    public class HistoryEdit : PatientBasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected TreatmentInfo TrmData;
        protected TreatmentHistoryInfo TrmHistoryData;

        protected new void HandleCommon()
        {
            this.AddLocation("治疗记录", "/TreatmentHistory/Index.aspx");
            this.AddLocation("修改治疗", "/TreatmentHistory/HistoryEdit.aspx");
            this.SetTitle("修改治疗");

            int id = RequestTools.GetInt("id", 0);
            if (id <= 0)
            {
                this.AddErrLine("请选择治疗历史信息");
                return;
            }


            TrmHistoryData = PatientTreatmentBLL.Instance.GetTreatmentRecorde(PrData.Id, id);
            if (TrmHistoryData.Id <= 0)
            {
                this.AddErrLine("错误的参数");
                return;
            }

            TrmData = TreatmentBLL.Instance.GetTreatmentInfo(TrmHistoryData.TreatmentId);
            if (TrmData.Id <= 0)
            {
                this.AddErrLine("错误的参数");
                return;
            }
        }

        protected new void HandleGet()
        {
        }

        protected new void HandlePost()
        {
            TreatmentHistoryInfo info = this.ValidateForm();
            if (info == null) return;
            bool succ = PatientTreatmentBLL.Instance.UpdateTreatmentRecorde(info);
            
            if (succ)
            {
                this.AddMsgLine("修改成功");
            }
            else
            {
                this.AddErrLine("修改失败");
            }

            this.SetUrl("/TreatmentHistory/List.aspx?trmid=" + TrmData.Id);
            this.SetMetaRefresh();
        }

        protected TreatmentHistoryInfo ValidateForm()
        {
            TreatmentHistoryInfo info = TrmHistoryData;

            // 对时间进行判断
            string value = "";
            DateTime date;
            bool succ = false;

            if (TrmHistoryData.State == Constants.CURRENT_USING)
            {
                value = RequestTools.GetFormString("startdate");
                if (value == "")
                {
                    this.AddErrLine("请填写日期！");
                    return null;
                }
                succ = DateTime.TryParse(value, out date);
                if (succ == false)
                {
                    this.AddErrLine("错误的日期：" + value);
                    return null;
                }
                info.StartDate = date;
                info.EndDate = DateTime.MaxValue;
                info.UsePurpose = RequestTools.GetFormInt("usepurpose", -1);
            }

            if (TrmHistoryData.State == Constants.STOPED_USE)
            {
                value = RequestTools.GetFormString("enddate");
                if (value == "")
                {
                    info.EndDate = DateTime.MaxValue;
                }
                else
                {
                    succ = DateTime.TryParse(value, out date);
                    if (succ == false)
                    {
                        this.AddErrLine("错误的日期：" + value);
                        return null;
                    }
                    info.EndDate = date;
                }
                info.DataValue = 0;
                info.StopReason = RequestTools.GetFormInt("stopreason", -1);
                info.StartDate = info.EndDate;
            }

            if (TrmHistoryData.State == Constants.CURRENT_USING || TrmHistoryData.State == Constants.CHANGE_DOSAGE)
            {
                info.DataValue = RequestTools.GetFormDouble("dosage", -1.0);
                if (info.DataValue < 0)
                {
                    this.AddErrLine("请填写正确的剂量 ");
                    return null;
                }
            } 
             //对时间进行检查
            if (DateTime.Compare(info.StartDate, DateTime.Today) > 0)
            {
                this.AddErrLine("填写的日期 " + info.StartDate.ToString("yyyy-MM-dd") + " 不能比当前日期 " + DateTime.Today.ToString("yyyy-MM-dd") + " 晚！");
                return null;
            }
            //else if (DateTime.Compare(date, PrData.StartDate) < 0)
            //{
            //    this.AddErrLine("填写的日期 " + date.ToString("yyyy-MM-dd") + " 不能比第一次发病日期 " + PrData.StartDate.ToString("yyyy-MM-dd") + " 早！");
            //    return null;
            //}

            //需要对判断是否有相同时间的记录（如果已经修改过时间）
            if (TrmHistoryData.StartDate.CompareTo(info.StartDate) != 0 && PatientTreatmentBLL.Instance.CheckTreatmentHistory(PrData.Id, TrmData.Id, info.StartDate))
            {
                //提示是否需要覆盖历史记录
                this.AddErrLine(info.StartDate.ToString("yyyy-MM-dd")+"已有的历史记录, 请删除后修改");
                return null;
            }

            info.UpdateDate = DateTime.Now;
            info.TreatmentId = TrmData.Id;
            info.PRId = PrData.Id;

            return info;
        }
    }
}